﻿#region Mortgage Controller
//
// All rights are reserved. Reproduction or transmission in whole or in part, in 
// any form or by any means, electronic, mechanical or otherwise, is prohibited 
// without the prior written consent of the copyright owner.
// 
// Filename: MortgageController.cs
//
#endregion 

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using DiscoveryLogic.UI.Layouts.Controls;
using DiscoveryLogic.DataServices.DataSources;
using DiscoveryLogic.UI.Layouts.Controls;

namespace DiscoveryLogic.UI.Layouts.Controllers
{
    /// <summary>
    /// Controller object associated with the mortgage calculator UI control.
    /// This control contains references to all IGraphControl objects (Views) added to the mortgage calculator and
    /// the mortgage object representing the users mortgage account (Model).
    /// 
    /// The controller responds to change events raised by the model or view objects and
    /// notifies the Views that they need to alter to reflect the changes by the other controls.
    /// </summary>
    public class MortgageController
    {
        /// <summary>
        /// holder for IGraphControl controls to be monitored and notified.
        /// </summary>
        private List<IGraphControl> controlledControls;

        /// <summary>
        /// holder for the mortgage account.
        /// </summary>
        private MortgageAccount model;

        /// <summary>
        /// Constructor
        /// </summary>
        public MortgageController()
        {
            controlledControls = new List<IGraphControl>();
            model = null;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="model">Mortgage Account</param>
        public MortgageController(MortgageAccount model):this()
        {
            SetModel(model);
        }

       
        /// <summary>
        /// Sets the current model (Mortgage Account).
        /// </summary>
        /// <param name="model">Mortgage Account</param>
        public void SetModel(MortgageAccount model)
        {
            this.model = model;
            if (model != null)
            {
                model.DetailChanged += new Account.DetailChangedEventHandler(model_DetailChanged);
            }
        }

        /// <summary>
        /// Adds an IGraphControl to the list of controlled controls.
        /// </summary>
        /// <param name="controlToAdd">a control to control.</param>
        public void AddControl(IGraphControl controlToAdd) {
            controlToAdd.SliderChanged += new SliderChangedEventHandler(controlToAdd_SliderChanged);
            controlledControls.Add(controlToAdd);

            // Take first control added and set initial values of all other controls
            if (controlledControls.Count > 1){
                double masterPosition = controlledControls[0].GetCurrentPosition();
                double masterSliderLength = controlledControls[0].GetSliderLength();
                double masterPercentage = masterPosition / masterSliderLength;

                for (int i = 1; i < controlledControls.Count; i++)
                {
                    controlledControls[i].SetSliderPercent(masterPercentage);                    
                }
            }
        }

        private double CalculateChangeInPayments(double deltaYears, double senderSliderLength, double targetSliderLength)
        {
            return (deltaYears / senderSliderLength) * targetSliderLength;
        }

        private double CalculateChangeInYears(double deltaPayments, double senderSliderLength, double targetSliderLength)
        {
            return (deltaPayments / senderSliderLength) * targetSliderLength;
        }
        /// <summary>
        /// Event handler for controlled IGraphControls SliderChangedEvents.
        /// </summary>
        /// <param name="sender">IGraphControl raising the event</param>
        /// <param name="e">event args</param>
        void controlToAdd_SliderChanged(object sender, EventArgs e)
        {
            double delta = ((IGraphControl)sender).GetLastSliderChange();

            if (controlledControls != null)
            {
                foreach (IGraphControl control in controlledControls)
                {
                    if (control != sender)
                    {
                        if (control.GetType() == typeof(BarChartCylindarAxis))
                        {
                            delta = CalculateChangeInPayments(delta, 
                                                              ((IGraphControl)sender).GetSliderLength(),
                                                              control.GetSliderLength());
                        }
                        else
                        {
                            delta = CalculateChangeInYears(delta,
                                                              ((IGraphControl)sender).GetSliderLength(),
                                                              control.GetSliderLength());
                        }
                        control.SetSlider(delta);
                    }
                }
            }
        }

        /// <summary>
        /// Event handler for data detail changes in the Mortgage Account( Model )
        /// </summary>
        /// <param name="sender">MortgageAccount raising the event</param>
        /// <param name="e">event args</param>
        void model_DetailChanged(object sender, EventArgs e)
        {
            if (controlledControls != null)
            {
                foreach (IGraphControl control in controlledControls)
                {
                    control.SetSlider(-2);
                }
            }
        }




    }
}
